//
//  VRPushStoryViewController.swift
//  VoiceRoom
//
//  Created by Rui on 2024/3/14.
//

import UIKit
import ZLPhotoBrowser

class VRPushStoryViewController: VRBaseViewController, SAAnimationProtocol {

    private let cancelButton = c_make_ex(UIButton.self)
        .text("Cancel").color(.white.alpha(0.4)).font(16.font_semiBold).end()
    private let postButton = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: .white.alpha(0.2)),state: .disabled)
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor)).color(.black)
        .color(.white.alpha(0.4),state: .disabled).font(16.font_semiBold)
        .corner(15).text("Post").end()
    private lazy var textView       = c_make_ex(UITextView.self)
        .placeholderFont(14.font_semiBold).color(.white).bgColor(.clear)
        .placeholderColor(.white.alpha(0.4)).font(14.font_semiBold)
        .placeholder("Record this moment and give it to those who understand you…").end()
    private let onePicButton = c_make_ex(UIButton.self)
        .image("Discover_pic").bgColor(.white.alpha(0.2))
        .corner(20).end()
    private let twoPicButton = c_make_ex(UIButton.self)
        .image("Discover_pic").bgColor(.white.alpha(0.2))
        .corner(20).end()
    private let oneDelButton = c_make_ex(UIButton.self)
        .image("Discover_del").end()
    private let twoDelButton = c_make_ex(UIButton.self)
        .image("Discover_del").end()
    private let selTagLabel = c_make_ex(UILabel.self)
        .text("Select tag").color(.white).font(18.font_bold).end()
    private let rightImageView = c_make_ex(UIImageView.self)
        .image("Discover_go").end()
    private let selTagButton = c_make_ex(UIButton.self).end()
    private let oneTagButton = c_make_ex(UIButton.self)
        .color("#FFFC00").font(14.font_medium).corner(15)
        .border("#FFFC00",width: 1).end()
    private let twoTagButton = c_make_ex(UIButton.self)
        .color("#FFFC00").font(14.font_medium).corner(15)
        .border("#FFFC00",width: 1).end()
    private let threeTagButton = c_make_ex(UIButton.self)
        .color("#FFFC00").font(14.font_medium).corner(15)
        .border("#FFFC00",width: 1).end()
    private var pushModel = VRPushStoryModel()
    
    
    private var oneImgUrl = ""
    private var twoImgUrl = ""
    private var tagString = ""
    
    init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .fullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            cancelButton
            postButton
            textView
            onePicButton.subviews{
                oneDelButton
            }
            twoPicButton.subviews{
                twoDelButton
            }
            selTagLabel
            rightImageView
            selTagButton
            oneTagButton
            twoTagButton
            threeTagButton
        }
        
        cancelButton.leading(15).top(.statusHeight + 12)
        postButton.trailing(15).top(.statusHeight + 7).width(70).height(30)
        textView.leading(15).trailing(15).top(.navi_height + 20).height(200)
        onePicButton.leading(15).size(82.5)
        onePicButton.Top == textView.Bottom + 10
        twoPicButton.leading(105).size(82.5)
        twoPicButton.Top == textView.Bottom + 10
        oneDelButton.top(8).trailing(8).size(30)
        twoDelButton.top(8).trailing(8).size(30)
        selTagLabel.leading(15)
        selTagLabel.Top == onePicButton.Bottom + 15
        rightImageView.trailing(15).size(22)
        rightImageView.CenterY == selTagLabel.CenterY
        selTagButton.leading(0).trailing(0).height(30)
        selTagButton.CenterY == rightImageView.CenterY
        oneTagButton.leading(15).height(30)
        oneTagButton.Top == rightImageView.Bottom + 15
        twoTagButton.height(30)
        twoTagButton.CenterY == oneTagButton.CenterY
        twoTagButton.Leading == oneTagButton.Trailing + 12
        threeTagButton.height(30)
        threeTagButton.CenterY == twoTagButton.CenterY
        threeTagButton.Leading == twoTagButton.Trailing + 12
        
        postButton.isEnabled = false
        oneDelButton.isHidden = true
        twoDelButton.isHidden = true
        twoPicButton.isHidden = true
        oneTagButton.isHidden = true
        twoTagButton.isHidden = true
        threeTagButton.isHidden = true
        
        oneTagButton.isUserInteractionEnabled = false
        twoTagButton.isUserInteractionEnabled = false
        threeTagButton.isUserInteractionEnabled = false
        oneTagButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        twoTagButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        threeTagButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        
        cancelButton.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        onePicButton.addTarget(self, action: #selector(oneImgAction), for: .touchUpInside)
        twoPicButton.addTarget(self, action: #selector(twoImgAction), for: .touchUpInside)
        oneDelButton.addTarget(self, action: #selector(oneDelAction), for: .touchUpInside)
        twoDelButton.addTarget(self, action: #selector(twoDelAction), for: .touchUpInside)
        selTagButton.addTarget(self, action: #selector(selTagAction), for: .touchUpInside)
        postButton.addTarget(self, action: #selector(postButtonAction), for: .touchUpInside)
    }
    
    private func updatePostButtonState(){
        
        guard let text = textView.text, text.count > 0 else {
            postButton.isEnabled = false
            return
        }
        if (oneImgUrl.count > 0 || twoImgUrl.count > 0) && tagString.count > 0 {
            postButton.isEnabled = true
        }else{
            postButton.isEnabled = false
        }
    }
    
}

//MARK: action
extension VRPushStoryViewController {
    
    @objc private func postButtonAction(){
        
        var imgUrl = oneImgUrl
        if twoImgUrl.count > 0 {
            imgUrl = imgUrl + "," + twoImgUrl
        }
        self.pushModel.icon = imgUrl
        self.pushModel.descInfo = textView.text
        self.pushModel.info = tagString
        self.pushModel.userId = USEINFOMODEL.id ?? ""
        self.pushModel.info1 = USEINFOMODEL.headImage ?? ""
        self.pushModel.info2 = USEINFOMODEL.name ?? ""
        self.pushModel.info3 = Date().toString(.yearMonthDay)
        self.pushModel.price = "0"
        self.pushModel.info0 = "VoiceRoom___*Story"
        self.pushModel.info9 = USEINFOMODEL.gender ?? "0"
        
        RT.createHouse(paraInfo: self.pushModel.kj.JSONObject()) { s in
            let successV = VRPushSuccessView()
            successV.dismissBlock = {
                self.dismiss(animated: true)
            }
            self.saAnimate.animation(show: successV)
        }
    }
    
    @objc private func selTagAction(){
        
        let deleteV = VRSelectTagView()
        deleteV.tagStringsBlock = { tagStrings in
            self.tagString = tagStrings
            if tagStrings.count > 0 {
                let arr = tagStrings.components(separatedBy: ",")
                if arr.count == 1 {
                    self.oneTagButton.isHidden = false
                    self.twoTagButton.isHidden = true
                    self.threeTagButton.isHidden = true
                    
                    self.oneTagButton.setTitle(arr.first, for: .normal)
                }else if arr.count == 2 {
                    self.oneTagButton.isHidden = false
                    self.twoTagButton.isHidden = false
                    self.threeTagButton.isHidden = true
                    
                    self.oneTagButton.setTitle(arr.first, for: .normal)
                    self.twoTagButton.setTitle(arr[1], for: .normal)
                }else{
                    self.oneTagButton.isHidden = false
                    self.twoTagButton.isHidden = false
                    self.threeTagButton.isHidden = false
                    
                    self.oneTagButton.setTitle(arr.first, for: .normal)
                    self.twoTagButton.setTitle(arr[1], for: .normal)
                    self.threeTagButton.setTitle(arr.last, for: .normal)
                }
            }
            self.updatePostButtonState()
        }
        saAnimate.animation(show: deleteV)
    }
    
    @objc private func oneImgAction(){
        //打开相册
        ZLPhotoConfiguration.default()
            .allowSelectVideo(false)
        let ps = ZLPhotoPreviewSheet()
        ps.selectImageBlock = { [weak self] results, isOriginal in
            ToastTool.show()
            let image = results.first?.image
            self?.onePicButton.setImage(image, for: .normal)
            self?.oneDelButton.isHidden = false
            self?.twoPicButton.isHidden = false
            RT.uploadCoverImage(file: (self?.imageToBase64String(image: image!))!) { [weak self] (dict) in
                ToastTool.dismiss()
                self?.oneImgUrl = dict.url!
                self?.updatePostButtonState()
            }
        }
        ps.showPhotoLibrary(sender: self)
    }
    
    @objc private func twoImgAction(){
        //打开相册
        ZLPhotoConfiguration.default()
            .allowSelectVideo(false)
        let ps = ZLPhotoPreviewSheet()
        ps.selectImageBlock = { [weak self] results, isOriginal in
            ToastTool.show()
            let image = results.first?.image
            self?.twoPicButton.setImage(image, for: .normal)
            self?.twoDelButton.isHidden = false
            RT.uploadCoverImage(file: (self?.imageToBase64String(image: image!))!) { [weak self] (dict) in
                ToastTool.dismiss()
                self?.twoImgUrl = dict.url!
                self?.updatePostButtonState()
            }
        }
        ps.showPhotoLibrary(sender: self)
    }
    
    @objc private func oneDelAction(){
        
        if twoImgUrl.count > 0 {
            oneImgUrl = twoImgUrl
            onePicButton.setImage(twoPicButton.imageView?.image, for: .normal)
            twoImgUrl = ""
            twoPicButton.setImage("Discover_pic".toImage, for: .normal)
        }else{
            oneImgUrl = ""
            onePicButton.setImage("Discover_pic".toImage, for: .normal)
        }
        
        oneDelButton.isHidden = true
        twoDelButton.isHidden = true
        twoPicButton.isHidden = true
        
        updatePostButtonState()
    }
    
    @objc private func twoDelAction(){
        
        twoImgUrl = ""
        twoPicButton.setImage("Discover_pic".toImage, for: .normal)
        
        twoDelButton.isHidden = true
        twoPicButton.isHidden = true
        
        updatePostButtonState()
        
    }
    
    @objc private func cancelAction(){
        self.dismiss(animated: true)
    }
    
}
